/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.tabbar;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.utils.LayoutAlignment;

import com.xuexiang.xui_lib.component.toast.XToast;
import com.xuexiang.xui_lib.tabbar.EasyIndicator;
import com.xuexiang.xui_lib.tabbar.tablayout.ContentPage;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.ability.EasyIndicatorFragmentAbility;
import com.xuexiang.xuidemo.slice.tabbar.provider.EasyIndicatorProvider;
import com.xuexiang.xuidemo.util.XToastUtils;

import java.util.ArrayList;

/**
 * EasyIndicator 简单指示器demo
 *
 * @since 2021-04-15
 */
public class EasyIndicatorSlice extends AbilitySlice implements Component.ClickedListener {
    private static final int Y_OFFSET = 100;
    private static final int TEST_INDEX = 2;

    boolean isFirst = true;
    EasyIndicator mEasyIndicator1;
    EasyIndicator mEasyIndicator;
    PageSlider mPageSlider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_easyindicator);
        mEasyIndicator1 = (EasyIndicator) findComponentById(ResourceTable.Id_easy_indicator1);
        mEasyIndicator = (EasyIndicator) findComponentById(ResourceTable.Id_easy_indicator);
        findComponentById(ResourceTable.Id_tab_bar_backImage).setClickedListener(this);
        findComponentById(ResourceTable.Id_fragment_btn).setClickedListener(this);
        mPageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);

        XToast.Config.get()
            // 位置设置为居中
            .setGravity(LayoutAlignment.BOTTOM)
            .setYOffset(Y_OFFSET);

        initIndicatorViewPager();
        initIndicatorNoViewPager();
    }

    private void initIndicatorViewPager() {
        mEasyIndicator.setTabTitles(ContentPage.getPageNames());
        EasyIndicatorProvider easyIndicatorProvider = new EasyIndicatorProvider(getData());
        mEasyIndicator.setPageSlider(mPageSlider);
        mPageSlider.setPageCacheSize(ContentPage.size() - 1);
        mPageSlider.setProvider(easyIndicatorProvider);
        mPageSlider.setPageSwitchTime(0);
        mPageSlider.setCurrentPage(TEST_INDEX);
    }

    private void initIndicatorNoViewPager() {
        mEasyIndicator1.setTabTitles(ContentPage.getPageNames());
        mEasyIndicator1.setOnTabClickListener(new EasyIndicator.OnTabClickListener() {
            @Override
            public void onTabClick(String title, int position) {
                // XToastUtils 的 bug
                if (isFirst) {
                    XToastUtils.toast("点击了" + title);
                    isFirst = true;
                }
                XToastUtils.toast("点击了" + title);
            }
        });
    }

    private ArrayList<EasyIndicatorProvider.DataItem> getData() {
        ArrayList<EasyIndicatorProvider.DataItem> dataItems = new ArrayList<>();

        String[] arr = ContentPage.getPageNames();
        for (int index = 0; index < arr.length; index++) {
            dataItems.add(new EasyIndicatorProvider.DataItem(String.format("这个是%s页面的内容", arr[index])));
        }
        return dataItems;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_fragment_btn:
                Intent newIntent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(getBundleName())
                    .withAbilityName(EasyIndicatorFragmentAbility.class.getName())
                    .build();

                // 把operation设置到intent中
                newIntent.setOperation(operation);
                startAbility(newIntent);
                break;
            case ResourceTable.Id_tab_bar_backImage:
                terminate();
                break;
            default:
                break;
        }
    }
}
